﻿using Corina.DataContext.Service;
using Corina.Datamodel.Base;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using Corina.Infrastructure;
using Corina.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.Configuration;

namespace Corina.DataContext.Master
{
    public class MasterBase : BaseClass
    {
        protected virtual string ConnectionString(DatabaseKeys key)
        {
            return WebConfigurationManager.ConnectionStrings[key.ToString()].ConnectionString;
        }

        protected T GetInstance<T>(params object[] paras) where T : BaseClass
        {
            return InstanceHelper.GetInstance<T>(paras);
        }

        protected ResultValue VerifyAccount<T>(string account, string password) where T : DatamodelEntityBase, new()
        {
            return GetInstance<AccountVerifyService>().VerifyAccount<T>(ConnectionString(DatabaseKeys.CORINA_ACCO), account, password);
        }

        protected T GetAccountInfo<T>(string account) where T : DatamodelEntityBase, new()
        {
            return GetInstance<AccountVerifyService>().GetAccountInfo<T>(ConnectionString(DatabaseKeys.CORINA_ACCO), account);
        }

        protected T GetAccountInfo<T>(int accountId) where T : DatamodelEntityBase, new()
        {
            return GetInstance<AccountVerifyService>().GetAccountInfo<T>(ConnectionString(DatabaseKeys.CORINA_ACCO), accountId);
        }

        protected bool ExistsAccount<T>(string account) where T : DatamodelEntityBase, new()
        {
            return GetInstance<AccountVerifyService>().ExistAccount<T>(ConnectionString(DatabaseKeys.CORINA_ACCO), account);
        }

        public bool ExistsEntity<T>(DatabaseKeys connectionKey, List<string> properties, List<object> values) where T : DatamodelEntityBase, new()
        {
            return GetInstance<ServiceBase>().Exists<T>(ConnectionString(connectionKey), properties, values);
        }

        protected RoleInfo GetManagerRole(int id)
        {
            return GetInstance<AccountVerifyService>().GetManagerRole(ConnectionString(DatabaseKeys.CORINA_BASE), id);
        }

        protected RoleInfo GetManagerRole(string account)
        {
            return GetInstance<AccountVerifyService>().GetManagerRole(ConnectionString(DatabaseKeys.CORINA_BASE), account);
        }

        protected UserLevelInfo GetUserLevel(int id)
        {
            return GetInstance<AccountVerifyService>().GetUserLevel(ConnectionString(DatabaseKeys.CORINA_BASE), id);
        }

        protected UserLevelInfo GetUserLevel(string account)
        {
            return GetInstance<AccountVerifyService>().GetUserLevel(ConnectionString(DatabaseKeys.CORINA_BASE), account);
        }

        protected AvatarInfo GetAccountAvatar(int accountId, AccountTypeValue accountType)
        {
            return GetInstance<AccountVerifyService>().GetAccountAvatar(ConnectionString(DatabaseKeys.CORINA_ACCO), accountId, accountType);
        }

        protected List<ObjectValueInfo> GetObjectOfValue(DataRow row, params string[] columns)
        {
            List<ObjectValueInfo> values = new List<ObjectValueInfo>();
            foreach (var column in columns)
            {
                values.Add(new ObjectValueInfo(column, row[column]));
            }

            return values;
        }
    }
}
